package com.webware.one.server.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/** Серверная реализация методов работы с датой */
public class DateUtil {

  public static String DATE_FORMAT = "dd.MM.yyyy";

  public static Date minDate() {
    return DateUtil.stringToDate("01.01.1000");
  }

  public static Date maxDate() {
    return DateUtil.stringToDate("01.01.3000");
  }

  public static Date today() {
    Date date = new Date();
    Calendar c = new GregorianCalendar();
    c.setTime(date);
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.HOUR_OF_DAY, 0);
    return c.getTime();
  }

  public static boolean isDate(String str) {
    DateFormat df = new SimpleDateFormat(DATE_FORMAT);
    try {
      df.parse(str);
      return true;
    }
    catch (Exception e) {
      return false;
    }
  }

  public static Date stringToDate(String str) {
    DateFormat df = new SimpleDateFormat(DATE_FORMAT);
    try {
      return df.parse(str);
    }
    catch (Exception e) {
      return null;
    }
  }

  public static String dateToString(Date date) {
    if (date == null)
      return null;
    else {
      DateFormat df = new SimpleDateFormat(DATE_FORMAT);
      return df.format(date);
    }
  }

  public static Integer getHours(Date date) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.HOUR_OF_DAY);
  }

  public static Date addHours(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.HOUR_OF_DAY, val);

    return calendar.getTime();
  }

  public static Date addMonths(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, val);

    return calendar.getTime();
  }

  public static Date addDays(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, val);

    return calendar.getTime();
  }

  public static Date addMinutes(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MINUTE, val);

    return calendar.getTime();
  }

  public static Date addMilliseconds(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MILLISECOND, val);

    return calendar.getTime();
  }

  public static Date addYears(Date date, int val) {
    if (date == null) return null;
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.YEAR, val);

    return calendar.getTime();
  }

  public static Long getAgeByDate(Date date) {
    if (date == null) return null;
    Calendar cDate = new GregorianCalendar();
    cDate.setTime(date);

    Calendar c = new GregorianCalendar();
    c.setTime(new Date());

    c.add(Calendar.DAY_OF_MONTH, -cDate.get(Calendar.DAY_OF_MONTH));
    c.add(Calendar.MONTH, -cDate.get(Calendar.MONTH));
    c.add(Calendar.YEAR, -cDate.get(Calendar.YEAR));
    return new Long(c.get(Calendar.YEAR));
  }

  public static Date getDateByAge(Long age) {
    if (age == null) return null;

    Calendar c = new GregorianCalendar();
    c.setTime(new Date());
    c.add(Calendar.YEAR, -age.intValue());
    return c.getTime();
  }

  public static Date updateAfterGWT(Date dateBirth) {
    if (dateBirth == null) return null;
    Calendar c = new GregorianCalendar();
    c.setTime(dateBirth);
    c.add(Calendar.HOUR_OF_DAY, 20);
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.HOUR_OF_DAY, 0);
    return c.getTime();
  }

  public static int getMonth(Date date) {
    if (date == null) return -1;
    return Integer.valueOf(new SimpleDateFormat("mm").format(date));
  }

  public static int getYear(Date date) {
    if (date == null) return -1;
    return Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
  }
}
